package com.example.demo.controller;


import com.example.demo.model.EnterpriseInfo;
import com.example.demo.model.EnterpriseInfoDTO;
import com.example.demo.repository.EnterpriseInfoRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by huang on 2018/1/9.
 */
@RequestMapping("/api/v1")
@RestController
@Transactional
public class EnterpriseInfoCRUDController extends BaseController {
    @Autowired
    EnterpriseInfoRepository enterpriseInfoRepository;
    @PostMapping("companies")
    public void insert(@PathVariable int id , @RequestBody EnterpriseInfoDTO enterpriseInfoDTO) {
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        BeanUtils.copyProperties(enterpriseInfoDTO, enterpriseInfo ,new String[]{"id"});//浅拷贝
        enterpriseInfoRepository.save(enterpriseInfo);

    }

    @GetMapping("companies")
    public ReturnDTO query(HttpServletRequest request0){
        PageRequest request1 = this.buildPageRequest(request0);
        Page<EnterpriseInfo> enterpriseInfo;
        String status = request0.getParameter("status");
        String creditCode = request0.getParameter("creditCode");
        String companyName = request0.getParameter("companyName");
        String[] createTime = request0.getParameterValues("createTime");
        String regAddress = request0.getParameter("regAddress");
        String companyType = request0.getParameter("companyType");
        enterpriseInfo= this.enterpriseInfoRepository.findAll(getWhereClause(regAddress, companyType, status, creditCode, companyName, createTime), request1);
        ReturnDTO  returnDTO = new ReturnDTO();
        returnDTO.setData(enterpriseInfo.getContent());
        returnDTO.setTotal(enterpriseInfo.getTotalElements());
        return returnDTO;

    }
    @GetMapping("companies/{id}")
    public EnterpriseInfoDTO queryById(HttpServletRequest request0, @PathVariable int id){
        EnterpriseInfo enterpriseInfo = enterpriseInfoRepository.findById(id);
        EnterpriseInfoDTO enterpriseInfoDTO = new EnterpriseInfoDTO();
        BeanUtils.copyProperties(enterpriseInfo, enterpriseInfoDTO);
        return enterpriseInfoDTO;

    }

    @PutMapping("companies/{id}")
    public void update(@PathVariable int id , @RequestBody EnterpriseInfoDTO enterpriseInfoDTO) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoRepository.findById(id);
        BeanUtils.copyProperties(enterpriseInfoDTO, enterpriseInfo, new String[]{"id"});//浅拷贝
        enterpriseInfoRepository.save(enterpriseInfo);
    }
    @DeleteMapping("companies/{id}")
    public void delete(@PathVariable int id) {
        enterpriseInfoRepository.deleteById(id);
    }
    private Specification<EnterpriseInfo> getWhereClause( final String regAddress, final String companyType, final String status, final String creditCode, final String companyName, final String[] createTime) {
        return new Specification<EnterpriseInfo>() {
            @Override
            public Predicate toPredicate(Root<EnterpriseInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();

                if (!StringUtils.isEmpty(status)) {
                    predicate.getExpressions().add(
                            criteriaBuilder.and(root.<String>get("status").in(status)));
                }
                if (!StringUtils.isEmpty(creditCode)) {
                    predicate.getExpressions().add(
                            criteriaBuilder.and(root.<String>get("creditCode").in(creditCode)));
                }
                if (!StringUtils.isEmpty(regAddress)) {
                    predicate.getExpressions().add(
                            criteriaBuilder.like(root.<String>get("regAddress"), "%"+regAddress+"%"));
                }
                if (!StringUtils.isEmpty(companyType)) {
                    predicate.getExpressions().add(
                            criteriaBuilder.like(root.<String>get("companyType"), "%"+companyType+"%"));
                }
                if (!StringUtils.isEmpty(companyName)) {
                    predicate.getExpressions().add(
                            criteriaBuilder.and(root.<String>get("companyName").in(companyName)));
                }
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                if (!StringUtils.isEmpty(createTime)) {
                    try {
                        predicate.getExpressions().add(
                                criteriaBuilder.between(root.<Date>get("createTime"), sdf.parse(createTime[0]), sdf.parse(createTime[1]))
                        );
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                return predicate;
            }
        };
    }

}
